package email

import (
	"ginblog/tools/log"
	"ginblog/utils"
	"github.com/sirupsen/logrus"
	"gopkg.in/gomail.v2"
	"mime"
	"strconv"
	"sync"
)

type Email struct {
	Title string
	Body  string
}

func (e *Email) GetEmailConfig() map[string]string {
	return map[string]string{
		"username": utils.MailUserName,
		"authCode": utils.MailAuthCode,
		"host":     utils.MailHost,
		"port":     utils.MailPort,
	}
}

func (e *Email) SendEmailList(mailTo string, wg *sync.WaitGroup) {
	defer wg.Done()
	mailConn := e.GetEmailConfig()
	port, _ := strconv.Atoi(mailConn["port"])
	m := gomail.NewMessage()
	m.SetHeader("From", mime.QEncoding.Encode("UTF-8", "Support")+"<"+mailConn["username"]+">")
	m.SetHeader("To", mailTo)
	m.SetHeader("Subject", e.Title)
	m.SetBody("text/html", e.Body)
	d := gomail.NewDialer(mailConn["host"], port, mailConn["username"], mailConn["authCode"])
	err := d.DialAndSend(m)
	if err != nil {
		log.Logger.Error("To:", mailTo, "##", "Send Email Failed!Err:", err)
	}
	log.Logger.Log(logrus.InfoLevel, "To:", mailTo, "##", "Send Email Successfully!")

}
